/*
 * author      : 吴中勤 
 * date        : 2019/1/6
 * description : TODO
 */


import java.io.IOException;

public class DeadLockThread {
    private static final Object lock1 = new Object();
    private static final Object lock2 = new Object();

    private static void creatDeadLock() {
        Thread t1 = new Thread(() -> {
            synchronized (lock1) {
                System.out.println("t1 start");
                try {
                    Thread.sleep(500);
                    synchronized (lock2) {
                        System.out.println("t1 goes ...");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "threadA, lock1 then lock2");

        Thread t2 = new Thread(() -> {
            synchronized (lock2) {
                System.out.println("t2 start");
                try {
                    Thread.sleep(500);
                    synchronized (lock1) {
                        System.out.println("t2 goes ...");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "threadB, lock2 then lock1");

        t1.start();
        t2.start();
    }


    public static void main(String[] args) throws IOException {
        System.out.println("begin");
        creatDeadLock();
        System.out.println("done");
    }
}